global FIRST_YEAR 1964
global FINAL_YEAR 2016

/*
1. Yields
*/

use "${dropbox_dir}combined_results_yu\data\fama_bliss_prices.dta", clear
gen D = mod(Date_yyyymmdd, 100)
gen M = mod((Date_yyyymmdd - D)/100, 100)
gen Y = (Date_yyyymmdd - 100*M - D)/10000
gen datem = ym(Y,M)
format datem %tm

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

local ave_yld = 0
local ave_sd = 0

forvalues year = 1/5{
	gen yld_`year'y = -100*log(price_`year'y/100)/`year'
	sum yld_`year'y
	local ave_yld = `ave_yld' + r(mean)
	local ave_sd = `ave_sd' + r(sd)
}

sum yld_*

local ave_yld = `ave_yld'/5
local ave_sd = `ave_sd'/5

disp `ave_yld'
disp `ave_sd'

/*
2. Equity premium 
*/

* using annual data
use "${dropbox_dir}combined_results_yu\data\FF_factors_annual.dta", clear
keep if year>=${FIRST_YEAR} & year<=${FINAL_YEAR}
sum MktRF

* monthly data
use "${dropbox_dir}combined_results_yu\data\FF_factors_monthly.dta", clear


* using monthly data
freduse USREC, clear
gen datem = ym(year(daten), month(daten))
format datem %tm

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\FF_factors_monthly.dta"
keep if _merge==3
drop _merge

save temp_dat.dta, replace

* use fama-bliss data for the risk free rate
use "${dropbox_dir}combined_results_yu\data\fama_bliss_prices.dta", clear
gen D = mod(Date_yyyymmdd, 100)
gen M = mod((Date_yyyymmdd - D)/100, 100)
gen Y = (Date_yyyymmdd - 100*M - D)/10000
gen datem = ym(Y,M)
format datem %tm

merge 1:1 datem using temp_dat.dta
keep if _merge==3
drop _merge

keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

sleep 5000
erase temp_dat.dta

* compute annualized returns from t to t+12
tsset datem

gen yld_1y = 100/price_1y - 1
gen mkt = (Mkt_RF + RF)/100
gen rf = RF/100
gen mkt_tF1_tF12 = (1+F1.mkt)*(1+F2.mkt)*(1+F3.mkt)*(1+F4.mkt)*(1+F5.mkt)*(1+F6.mkt)*(1+F7.mkt)*(1+F8.mkt)*(1+F9.mkt)*(1+F10.mkt)*(1+F11.mkt)*(1+F12.mkt)-1
gen rf_tF1_tF12 = (1+F1.rf)*(1+F2.rf)*(1+F3.rf)*(1+F4.rf)*(1+F5.rf)*(1+F6.rf)*(1+F7.rf)*(1+F8.rf)*(1+F9.rf)*(1+F10.rf)*(1+F11.rf)*(1+F12.rf)-1
gen mkt_excess_ret1 = mkt_tF1_tF12 - yld_1y
gen mkt_excess_ret2 = mkt_tF1_tF12 - rf_tF1_tF12

sum mkt_excess_ret* USREC
sum mkt_excess_ret* if USREC==0
sum mkt_excess_ret* if USREC==1

/*
3. D/P ratio
*/

* monthly frequency
freduse USREC, clear
gen datem = ym(year(daten), month(daten))
format datem %tm

merge 1:1 datem using "${dropbox_dir}combined_results_yu\data\crsp_returns.dta"
keep if _merge==3
drop _merge

drop DP
gen DP = 12*(ret - retx)/(1 + retx)
gen PD = 1/DP

tsset datem
keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

sum DP PD
sum DP PD if USREC==1
sum DP PD if USREC==0

/*
* 4. Bond (log) holding period returns
*/

freduse USREC, clear
gen datem = ym(year(daten), month(daten))
format datem %tm
keep datem USREC
save temp_USREC.dta


use "${dropbox_dir}combined_results_yu\data\fama_bliss_prices.dta", clear
gen D = mod(Date_yyyymmdd, 100)
gen M = mod((Date_yyyymmdd - D)/100, 100)
gen Y = (Date_yyyymmdd - 100*M - D)/10000
gen datem = ym(Y,M)
format datem %tm

merge 1:1 datem using temp_USREC.dta
keep if _merge==3
drop _merge

sleep 5000
erase temp_USREC.dta

tsset datem
keep if datem>=ym(${FIRST_YEAR},1) & datem<=ym(${FINAL_YEAR},12)

forvalues n = 2/5{
    local nsell = `n' - 1
    gen hpxr_`n'y = log(price_`nsell'y/100) - log(L12.price_`n'y/100) + log(L12.price_1y/100)
}

sum hpxr*
sum hpxr* if L12.USREC==0
sum hpxr* if L12.USREC==1